package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.common.utils.HashMapUtils;
import com.j283_5.wisdom_park.mapper.RoleinfoMapper;
import com.j283_5.wisdom_park.pojo.Project;
import com.j283_5.wisdom_park.pojo.Roleinfo;
import com.j283_5.wisdom_park.service.RoleinfoService;
import com.j283_5.wisdom_park.vo.confcenter_management.RoleinfoVo;
import com.j283_5.wisdom_park.vo.contract_management.CustomerNeedsVo;
import com.j283_5.wisdom_park.vo.contract_management.CustomerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.management.relation.Role;
import javax.management.relation.RoleInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class RoleinfoServiceImpl extends ServiceImpl<RoleinfoMapper, Roleinfo> implements RoleinfoService {
    @Resource
    private RoleinfoMapper roleinfoMapper;

    @Override
    public ResultData getRolePage(Page<Roleinfo> page,String roName){
        if(page==null){
            page = new Page<>(1,10);
        }
        QueryWrapper<Roleinfo> roleInfoQueryWrapper = new QueryWrapper<>();
        roleInfoQueryWrapper.orderByDesc("ro_updatetime");
        if(roName != null){
            roleInfoQueryWrapper.like("ro_name",roName);
        }
        Page<Roleinfo> roleinfoPage = roleinfoMapper.selectPage(page, roleInfoQueryWrapper);
        if(roleinfoPage != null){
            return ResultData.ok().data(roleinfoPage);
        }else{
            return ResultData.error().message("查询失败!");
        }
    }

    @Override
    public ResultData addRoleInfo(RoleinfoVo roleinfoVo) {
        Roleinfo roleinfo=new Roleinfo();
        BeanUtils.copyProperties(roleinfoVo,roleinfo);
        int i = roleinfoMapper.insert(roleinfo);
        if (i>0){
            return ResultData.ok().message("添加成功！");
        }
        return ResultData.error().message("添加失败!");
    }

    @Override
    public ResultData deleteRoleInfo(ArrayList<Integer> roIdlist) {
        UpdateWrapper<Roleinfo> roleinfoUpdateWrapper=new UpdateWrapper<>();
        roleinfoUpdateWrapper.set("ro_state",1);
        roleinfoUpdateWrapper.in("ro_id",roIdlist);
        int i=roleinfoMapper.update(null,roleinfoUpdateWrapper);
        if (i>0){
            return ResultData.ok().message("删除成功!");
        }
        return ResultData.ok().message("删除失败!");
    }

    @Override
    public ResultData updateRoleInfo(Roleinfo roleinfo) {
        UpdateWrapper<Roleinfo> roleinfoUpdateWrapper=new UpdateWrapper<>();
        roleinfoUpdateWrapper.eq("ro_id",roleinfo.getRoId())
                .set("ro_name",roleinfo.getRoName())
                .set("ro_type",roleinfo.getRoType())
                .set("ro_state",roleinfo.getRoState())
                .set("ro_type",roleinfo.getRoType())
                .set("ro_updatetime",roleinfo.getRoUpdatetime());

        int i=roleinfoMapper.update(null,roleinfoUpdateWrapper);
        if (i>0){
            return ResultData.ok().message("修改成功!");
        }
        return ResultData.ok().message("修改失败!");
    }
}
